package com.kfm.pm.udp;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class Client {

    public static void main(String[] args) throws IOException {
        System.out.println("客户端启动成功");
        // 输入内容发送
        Scanner sc = new Scanner(System.in);
        DatagramSocket socket = new DatagramSocket(7788); // 客户端启动在7788端口

        // 客户端输入 exit 客户端就结束
        while (true) {
            System.out.println("你说：");
            String msg = sc.next();
            if ("exit".equals(msg)) {
                socket.close();
                System.out.println("退出成功，欢迎下次使用！");
                break;
            }
            byte[] bytes = msg.getBytes();
            InetAddress address = InetAddress.getByName("127.0.0.1");
            DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, address, 8888);
            socket.send(packet);
            byte[] bytes1 = new byte[1024 * 60];
            DatagramPacket packet1 = new DatagramPacket(bytes1, 0, bytes1.length);
            socket.receive(packet1);
            System.out.println("服务端回复：" + new String(bytes1, 0, packet1.getLength()));
        }
    }
}
